﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Compression;

namespace ConsoleApp.Web
{
    public class Add_GZip_InAshx
    {
        public static void demo()
        {
            System.Web.HttpContext hc = System.Web.HttpContext.Current;
            string acceptEncoding = hc.Request.Headers["Accept-Encoding"].ToString().ToUpperInvariant();
            if (!String.IsNullOrEmpty(acceptEncoding))
            {
                //如果头部里有包含"GZIP","DEFLATE",表示你浏览器支持GZIP,DEFLATE压缩
                if (acceptEncoding.Contains("GZIP"))
                {
                    //向输出流头部添加压缩信息
                    hc.Response.AppendHeader("Content-encoding", "gzip");
                    hc.Response.Filter = new GZipStream(hc.Response.Filter, CompressionMode.Compress);
                }
                else if (acceptEncoding.Contains("DEFLATE"))
                {
                    //向输出流头部添加压缩信息
                    hc.Response.AppendHeader("Content-encoding", "deflate");
                    hc.Response.Filter = new DeflateStream(hc.Response.Filter, CompressionMode.Compress);
                }
            }
        }
    }
}
